package com.yjy.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.BaseUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.SysMenuMapper;
import com.system.common.mapper.SysRoleMenuMapper;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysMenuExample;
import com.system.common.pojo.SysRoleMenu;
import com.system.common.pojo.SysRoleMenuExample;
import com.system.common.pojo.SysRoleMenuExample.Criteria;

@Service
public class SysMenuServiceImpl implements SysMenuService {
	
	@Autowired
	private SysMenuMapper menuMapper;
	
	@Autowired
	private SysRoleMenuMapper rmMapper;

	@Override
	public List<SysMenu> selectMenu(String roleId) {
		List<SysMenu> menus=new ArrayList<>();
		SysRoleMenuExample example=new SysRoleMenuExample();
		Criteria cri=example.createCriteria();
		cri.andRoleIdEqualTo(roleId);
		List<SysRoleMenu> list=rmMapper.selectByExample(example);
		for (SysRoleMenu rm : list) {
			SysMenu menu=menuMapper.selectByPrimaryKey(rm.getMenuId());
			if (menu.getMenuType().equals("1")) {
				menus.add(menu);
			}			
		}
		return menus;
	}

	@Override
	public List<SysMenu> selectMenu() {
		List<SysMenu> menus = new ArrayList<SysMenu>();
		 List<SysMenu>  list= menuMapper.selectByExample(new SysMenuExample());
		 for (SysMenu sysMenu : list) {
			if (sysMenu.getMenuType().equals("1")) {
				menus.add(sysMenu);
			}
		}
		return menus;
	}

	@Override
	public List<SysMenu> listAll(Map<String, Object> map) {
		String mname= (String) map.get("mname");
		SysMenuExample example=new SysMenuExample();
		com.system.common.pojo.SysMenuExample.Criteria cri=example.createCriteria();
		if (StringUtils.isNotBlank(mname)) {
			cri.andMenuNameLike("%"+mname+"%");
		}
		cri.andMenuTypeEqualTo("1");
		return menuMapper.selectByExample(example);
	}

	@Override
	public int delete(String menuid) {
		SysMenuExample example=new SysMenuExample();
		com.system.common.pojo.SysMenuExample.Criteria criteria=example.createCriteria();
		if (StringUtils.isNotBlank(menuid)) {
			criteria.andParentIdEqualTo(menuid);
		}
		List<SysMenu>  list=menuMapper.selectByExample(example);
		if (!list.isEmpty()) {//如果这个集合不是null的
			return 0;
		}else {
			menuMapper.deleteByPrimaryKey(menuid);
			return 1;
		}
	}

	@Override
	public List<SysMenu> queryMenuIds(String ids) {
		SysMenuExample example=new SysMenuExample();
		List<SysMenu>  menus=new ArrayList<>();
		if (ids.equals("0")) {
			menus=menuMapper.selectByExample(example);
		}else {
			menus=menuMapper.queryMenuIds(ids);
		}
		return menus;
	}

	@Override
	public List<SysMenu> queryParentId() {
		SysMenuExample example=new SysMenuExample();
		com.system.common.pojo.SysMenuExample.Criteria criteria=example.createCriteria();
		//criteria.andParentIdEqualTo("0");
		criteria.andMenuTypeEqualTo("1");
	    List<SysMenu> menus=menuMapper.selectByExample(example);
		return menus;
	}

	@Override
	public void save(SysMenu menu) {
		Snowflake snow=new Snowflake(0,0);
		menu.setMenuId(snow.nextId()+"");
		this.getCascadeId(menu);
		menuMapper.insert(menu);		
	}
	
	public void getCascadeId(SysMenu menu) {
		// 获取节点语义id
		String cascadeId = menuMapper.queryBycascadeId(menu.getParentId());// 通过当前要添加的节点的父节点去查询当前父节点的最大节点语义id
		if (cascadeId == null || cascadeId.equals("")) {// 如果等于null或者等于“”，说明当前添加的父节点没有子节点
			SysMenu sys = menuMapper.selectByPrimaryKey(menu.getParentId());// 然后查询当前父节点的语义节点id
			menu.setCascadeId(sys.getCascadeId() + ".001");// 在拼接字符串
		} else {
			String casc = BaseUtils.treeId(cascadeId, 999);
			menu.setCascadeId(casc);
		}
	}

	@Override
	public String toselectParentname(String parentId) {
		SysMenu menu=menuMapper.selectByPrimaryKey(parentId);
		if (menu!=null) {
			return menu.getMenuName();
		}
		return null;
	}

	@Override
	public void update(SysMenu menu) {
		this.getCascadeId(menu);
		SysMenu m=menuMapper.selectByPrimaryKey(menu.getMenuId());
		menu.setCreateUserId(m.getCreateUserId());
		menu.setCreateTime(m.getCreateTime());
		menuMapper.updateByPrimaryKeySelective(menu);
		
	}

}
